package io.esirong.widget.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import java.lang.ref.WeakReference;

import androidx.annotation.LayoutRes;
import io.esirong.widget.R;

/**
 * 弹窗对话框<br>
 * 显示默认的信息弹窗（提示内容+确认按钮）
 * <P>如果你需要更复杂的显示，使用
 */
public class DialogUtil {
    /**
     * 默认类型
     */
    public static final int TYPE = 1;
    private Boolean button = true;

    private WeakReference<Activity> mRefActivity;
    private Dialog mDialog;

    private DialogUtil(Activity activity) {
        this.mRefActivity = new WeakReference<>(activity);
    }

    public static DialogUtil with(Activity activity) {
        return new DialogUtil(activity);
    }

    /**
     * 不显按钮
     *
     * @return
     */
    public DialogUtil noButton() {
        button = false;
        return this;
    }

    /**
     * 在当前显示一个弹窗
     *
     * @param message 提示的内容
     * @param close   true 显示可以点击关闭当前页，false 不显示不关闭当前页
     */
    public void showDialog(String message, final boolean close) {
        final Dialog dialog = createDialog(R.layout.dialog_layout);

        //设置dialog的内容
        TextView textView = dialog.findViewById(R.id.dialog_text);
        textView.setText(message);

        Button button = dialog.findViewById(R.id.btn_dialog_button_yes);
        if (!close) {
            button.setVisibility(View.GONE);
        }
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                if (close) {
                    Activity a = mRefActivity.get();
                    if (a != null) {
                        a.finish();
                    }

                }
            }
        });

        dialog.show();
    }

    /**
     * 显示自定义的dialog内容
     *
     * @param layoutId 显示的内容
     */
    public void showDialog(@LayoutRes int layoutId) {
        createDialog(layoutId).show();
    }

    /**
     * 显示自定义的dialog内容
     *
     * @param view 显示的内容
     */
    public void showDialog(View view) {
        mDialog = new Dialog(mRefActivity.get());
        mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mDialog.setContentView(view);
        initDialog(mDialog).show();
    }

    /**
     * 隐失
     */
    public void dismissDialog() {
        if (mDialog != null) {
            mDialog.dismiss();
        }

    }

    private Dialog createDialog(int layout) {
        mDialog = new Dialog(mRefActivity.get());
        mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mDialog.setContentView(layout);
        return initDialog(mDialog);
    }

    private Dialog initDialog(Dialog dialog) {
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        dialog.getWindow().setAttributes(lp);
        //自定义背景颜色
        dialog.getWindow().getDecorView().setBackgroundResource(0x00000000);
        return dialog;
    }
}